package imu.g3.controller;


import imu.g3.model.LoginLogInfo;
import imu.g3.model.MyProjectUploadFiles;
import imu.g3.model.User;
import imu.g3.service.FileService;
import imu.g3.service.SystemLogService;
import imu.g3.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private FileService fileService;
    @Autowired
    private SystemLogService systemLogService;


    //注册
    @RequestMapping(value = "/register")
    public String register(User user){
        int flag = userService.register(user);
        return "login";

    }

    //验证方法、注册时候用来验证信息
    @RequestMapping(value = "/validateInfo")
    @ResponseBody
    public void validateInfo(String name, String phone,HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String res = "";
        List<User> userList = userService.getUserInfo();
        int flag = 0;
        for (User user:userList) {
            if( name == user.getName() || name.equals(user.getName())){
                resp.getWriter().write("nameis");
                flag = 1;
                break;

            }
            if(phone == user.getPhone() || phone.equals(user.getPhone())){
                resp.getWriter().write("phis");
                flag = 1;
                break;
            }

        }
        if(flag == 0){
            resp.getWriter().write("yes");
        }
    }

    @RequestMapping("/toLogin")
    public String initLogin() {
        return "login";
    }
    //登录验证
    @RequestMapping(value = "/loginVal")
    @ResponseBody
    public void loginVal(String username, String password,HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String res = "";
        List<User> userList = userService.getUserInfo();
        boolean nameNotNull=false,phoneNotNull=false;

        int flag = 0;
        for (User user:userList) {
            //说明用户存在
            if( (username == user.getName() || username.equals(user.getName()))
                    || (username == user.getPhone() || username.equals(user.getPhone()))){
                if(password == user.getPassword() || password.equals(user.getPassword())){
                    flag = 2;
                    //放到session中,用来验证是否已经登录成功
                    req.getSession().setAttribute("flag","loginsucc");
                    req.getSession().setAttribute("User",user);

                    //直接从项目中获取文件，显示到前台
                    String realpath = req.getServletContext().getRealPath("uploadfiles");
                    File dir = new File(realpath);
                    File files[] = dir.listFiles();
                    //获取该目录下的所有文件名
                    ArrayList<String> fileName = new ArrayList<String>();
                    ArrayList<String> fileNameFromDataSet = new ArrayList<String>();
                    ArrayList<LoginLogInfo> loginLogDataSet = new ArrayList<LoginLogInfo>();
                    if(files != null){
                        for (int i = 0; i < files.length; i++){
                            fileName.add(files[i].getName());
                        }
                    }

                    //从数据库中获取文件，数据库中的文件名称都是可以下载的文件
                    ArrayList<MyProjectUploadFiles> filesArrayList = (ArrayList<MyProjectUploadFiles>) fileService.getAllFiles();
                    if(filesArrayList!=null){
                        if(filesArrayList.size()>10){
                            for(int i=0;i<10;i++){
                                MyProjectUploadFiles uploadFile = filesArrayList.get(i);
                                fileNameFromDataSet.add(uploadFile.getFileName());
                            }
                        } else{
                            for(int i=0;i<filesArrayList.size();i++){
                                MyProjectUploadFiles uploadFile = filesArrayList.get(i);
                                fileNameFromDataSet.add(uploadFile.getFileName());
                            }
                        }

                    }

                    //从数据库中获取登录日志
                    ArrayList<LoginLogInfo> loginLogArrayList = (ArrayList<LoginLogInfo>) systemLogService.getLoginLog();
                    req.getSession().setAttribute("allLoginLogList",loginLogArrayList);

                    if(loginLogArrayList!=null){
                        if(loginLogArrayList.size()>15){
                            for(int i=0;i<15;i++){
                                LoginLogInfo loginLogInfo = loginLogArrayList.get(i);
                                loginLogDataSet.add(loginLogInfo);
                            }
                        } else{
                            for(int i=0;i<loginLogArrayList.size();i++){
                                LoginLogInfo loginLogInfo = loginLogArrayList.get(i);
                                loginLogDataSet.add(loginLogInfo);
                            }
                        }

                    }
                    req.getSession().setAttribute("loginLogList",loginLogDataSet);
                    req.getSession().setAttribute("files",fileNameFromDataSet);
                    req.getSession().setAttribute("path",realpath);
                    userService.loginSuccess(user);
                    resp.getWriter().write("yes");
                    break;
                }else{
                    flag = 1;
                    resp.getWriter().write("pwderr");
                    break;
                }
            }

        }
        if(flag == 0){
            resp.getWriter().write("noname");
        }
    }
    @RequestMapping(value = "/my")
    public String my(User user){
        return "my";
    }

    @RequestMapping(value = "loguot")
    public String logout(HttpSession session){
        //清除session
        session.invalidate();
        return  "login";

    }


}
